package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<>();
        shoppingCart.setUserId(userId);
        qw.eq(ShoppingCart::getUserId,userId);
        //菜品or 套餐
        if(shoppingCart.getDishId()!=null){
            //菜品
            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

        }else{
            //套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(qw);
        if(one==null){
            //不存在
            shoppingCartService.save(shoppingCart);
            one=shoppingCart;

        }else{
            //已存在
            Integer number = one.getNumber();
            one.setNumber(number+1);
            shoppingCartService.updateById(one);
        }



        return R.success(one);
    }

    /**
     * 查找
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<>();
        Long currentId = BaseContext.getCurrentId();
        qw.eq(ShoppingCart::getUserId,currentId);
        List<ShoppingCart> list = shoppingCartService.list(qw);

        return R.success(list);
    }

    /**
     * 删除部分
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        qw.eq(ShoppingCart::getUserId,userId);
        if (dishId!=null){
            qw.eq(ShoppingCart::getDishId,dishId);
        }else{
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        Integer number = one.getNumber();
        if(number==1)
            shoppingCartService.remove(qw);
        else{
            one.setNumber(number-1);
            shoppingCartService.updateById(one);
        }
        return R.success(shoppingCart);
    }


    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> qw=new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        qw.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(qw);
        return R.success("清空购物车成功");
    }
}